package com.dtw.ditouwang.ui.coin

import android.content.DialogInterface
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.dtw.ditouwang.R
import com.dtw.ditouwang.beans.ProductBean
import com.dtw.ditouwang.ui.BaseActivity
import com.dtw.ditouwang.ui.OnItemClickListener
import kotlinx.android.synthetic.main.activity_coin.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class ProductListActivtiy : BaseActivity(), OnItemClickListener, View.OnClickListener {

    var procuctList = ArrayList<ProductBean.DataBean>();
    var coinAdapter = CoinAdapter(procuctList)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        setContentView(R.layout.activity_coin)
        setTitle("金币兑换")
        setSubTitle("已对换", this)
        enableBack()

        recyclerView.layoutManager = GridLayoutManager(this, 2)
        recyclerView.adapter = coinAdapter
        coinAdapter.onItemClickListener = this
        webApi.product.enqueue(object : Callback<ProductBean> {
            override fun onFailure(call: Call<ProductBean>, t: Throwable) {
                println("fail")
            }

            override fun onResponse(call: Call<ProductBean>, response: Response<ProductBean>) {
                var productBean = response.body()
                if (productBean != null) {
                    procuctList.clear()
                    procuctList.addAll(productBean.data)
                    coinAdapter.notifyDataSetChanged()
                }
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.text_sub_title -> {
                startActivity(Intent(this, OrderListActivity::class.java))
            }
        }
    }

    override fun onItemClick(resID: Int, position: Int, state: Int) {
        var intent = Intent(this, ProductDetialActivity::class.java)
        intent.putExtra(ProductDetialActivity.KeyProductID, procuctList[position].id)
        startActivity(intent)
    }
}
